package cn.wen.yaling.controller;

import java.util.List;
import java.util.Map;

import cn.wen.yaling.commons.entity.TypeEntity;
import cn.wen.yaling.commons.enums.HttpCode;
import cn.wen.yaling.commons.vo.CommentVO;
import cn.wen.yaling.commons.vo.QuestionAddVO;
import cn.wen.yaling.commons.vo.QuestionListVO;
import cn.wen.yaling.commons.vo.QuestionUpdateVO;
import cn.wen.yaling.xo.service.CommentService;
import cn.wen.yaling.xo.service.TypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.commons.entity.QuestionsEntity;
import cn.wen.yaling.xo.service.QuestionsService;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.ResultEntity;


/**
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:37
 */
@RestController
public class QuestionsController {

    @Autowired
    private QuestionsService questionsService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private CommentService commentService;

    /**
     * 获取全部的问答列表
     *
     * @return
     */
    @GetMapping("/web/question/list")
    public ResultEntity getAllIsActiveQuestionList() {
        List<QuestionListVO> questionListVOS = questionsService.getAllIsActiveQuestionList();
        return ResultEntity.ok().put("data", questionListVOS);
    }

    /**
     * 发布一个帖子
     * 发布帖子需要实现邮箱、密码（MD5加密）、内容、照片、备注
     *
     * @param question
     * @return
     */
    @PostMapping("/web/publish/question")
    public ResultEntity publishQuestion(@RequestBody QuestionAddVO question, @RequestParam("typeId") Integer typeId) {
        // 直接到业务层实现
        return questionsService.publishQuestion(question, typeId);

    }

    /**
     * 获取问题的类别列表
     *
     * @return
     */
    @GetMapping("/web/question/type")
    public ResultEntity getQuestionType() {
        List<TypeEntity> typeEntities = typeService.getQuestionType();
        return ResultEntity.ok().put("data", typeEntities);
    }

    /**
     * 获取最热问答列表
     *
     * @return
     */
    @GetMapping("/web/question/hot-list")
    public ResultEntity getHotQuestionList() {
        List<QuestionsEntity> questionHotList = questionsService.getHotQuestionList();
        return ResultEntity.ok().put("data", questionHotList);
    }

    /**
     * 通过问答Id获取
     *
     * @return
     */
    @GetMapping("/web/question-info")
    public ResultEntity getQuestionInfoByQuestionId(@RequestParam("questionId") Integer questionId) {
        QuestionsEntity questionsEntity = questionsService.getQuestionInfoByQuestionId(questionId);
        return ResultEntity.ok().put("data", questionsEntity);
    }

    /**
     * 获取评论列表
     *
     * @return
     */
    @GetMapping("/web/question/comment-list")
    public ResultEntity getQuestionCommentList(@RequestParam("questionId") Integer questionId) {
        List<CommentVO> commentVOS = commentService.listCommentByQuestionId(questionId);
        return ResultEntity.ok().put("data", commentVOS);
    }

    /*================================= 后台 API ====================================*/

    /**
     * 获取全部的问答列表
     * @return
     */
    @GetMapping("/admin/question/list")
    public ResultEntity getAllQuestionList() {
        return questionsService.getAllNotDeleteQuestionList();
    }

    /**
     * 获取全部的的问答分类
     * @return
     */
    @GetMapping("/admin/question/type/list")
    public ResultEntity getAllQuestionTypeList() {
        return questionsService.getQuestionTypeList();
    }

    /**
     * 获取全部的问答列表
     * @return
     */
    @GetMapping("/admin/question/search/list")
    public ResultEntity searchAllQuestionList(@RequestParam("keyword") String keyword) {
        return questionsService.searchAllNotDeleteQuestionList(keyword);
    }

    /**
     * 增加问答信息
     * @return
     */
    @PostMapping("/admin/question/add")
    public ResultEntity addQuestion(@RequestBody QuestionAddVO question) {
        return questionsService.addQuestion(question);
    }

    /**
     * 修改问答信息
     * @return
     */
    @PostMapping("/admin/question/update")
    public ResultEntity updateQuestion(@RequestBody QuestionUpdateVO question) {
        return questionsService.updateQuestion(question);
    }

    /**
     * 批量删除问答信息
     * @return
     */
    @PostMapping("/admin/question/delete")
    public ResultEntity deleteQuestion(@RequestParam("questionIds") Integer[] questionIds) {
        return questionsService.deleteBatchQuestion(questionIds);
    }

    /**
     * 修改问答信息
     * @return
     */
    @PostMapping("/admin/question/update-publish")
    public ResultEntity updateQuestion(@RequestParam("questionId") Integer questionId,
                                       @RequestParam("status") Integer status) {
        return questionsService.updateQuestionStatus(questionId, status);
    }

    /**
     * 修改问答是否开启评论
     * @return
     */
    @PostMapping("/admin/question/update-comment")
    public ResultEntity updateQuestion(@RequestParam("questionId") Integer questionId,
                                       @RequestParam("isComment") boolean isComment) {
        return questionsService.updateQuestionIsComment(questionId, isComment);
    }


}
